Make source-map-support work when the Object prototype is frozen#331
Make source-map-support work when the Object prototype is frozen#331jportner wants to merge 3 commits intoevanw:masterfrom
Conversation
LinusU
left a comment
There was a problem hiding this comment.
The changes looks good to me 👍
Can you think of anything that would prevent this being released as a new patch version?
Good question! The existing approach that sets this property using the On the other hand, this new approach using I don't think it's likely, but I suppose there's a chance that some projects could be changing the I pushed 1a14441 to make this change. With that, I think this is definitely safe to release as a patch version 👍 |
|
@LinusU if you don't have any other concerns, would you be willing to merge/release this change? 🙏 |
Forgot to do this when I added 1a14441
Background
Note: examples use the Node.js REPL with strict mode:
node --use_strict.Inheritance and Shadowing
Objects in JavaScript inherit properties from their prototype chain. For example, the "toString" property can be accessed on all objects, but it doesn't actually exist on each object, it exists on the global Object prototype:
Under normal circumstances, you can assign a property to an object using the
=operator, and any property of the same name in the object's prototype chain will not be modified, but will be "shadowed" by the new property:Prototype Pollution
From Snyk:
There are a few different ways to mitigate Prototype Pollution, and one way to do it across the board is to freeze the global "root" objects and their prototypes (Object, Function, Array, etc.)
From MDN:
This means that any attempt to change the Object prototype will fail. If using strict mode, it will throw an error; otherwise, it will be silently ignored.
If the Object prototype becomes frozen, all of its properties are no longer writable or configurable:
This also prevents shadowing properties with assignment. If an object doesn't already have a property defined (such as "toString"), and it inherits a non-writable property of that name from its prototype chain, any attempt to assign the property on that object will fail:
This behavior is described in the ECMAScript 2016 specification:
The Problem
Unfortunately, this package uses assignment to shadow the "toString" function for call site frames of stack traces:
node-source-map-support/source-map-support.js
Line 364 in 7b5b81e
This means that projects cannot use this package if they have frozen the global Object prototype.
The Solution
You can still shadow non-writable prototype properties by explicitly defining a new data property on the object:
The cloneCallSite function can be changed to use this method of shadowing so it is compatible with this approach of mitigating Prototype Pollution 🎉